<?php

namespace App\Admin\Forms;

use Dcat\Admin\Admin;
use Dcat\Admin\Widgets\Form;
use App\Admin\Field\IntervalWeekNum;
use App\Models\HospitalAppointInterval;
use App\Models\HospitalAppointIntervalNum;
use App\Models\HospitalAppointStreet;
use App\Models\Street;
use Illuminate\Support\Facades\DB;

class HospitalAppointStreetForm extends Form
{
    /**
     * Handle the form request.
     *
     * @param array $input
     *
     * @return mixed
     */
    public function handle(array $input)
    {
        $hospital_appoint_id = $input['hospital_appoint_id'];
        $hospital_id = $input['hospital_id'];

        $street = HospitalAppointStreet::query()
        ->where('hospital_id',$hospital_id)
        ->where('hospital_appoint_id',$hospital_appoint_id)
        ->first();
        if(!$street){
            $street = new HospitalAppointStreet();
        }
        $street->hospital_id = $hospital_id;
        $street->hospital_appoint_id = $hospital_appoint_id;
        $street->street = $input['street'];
        $street->save();

        return $this->response()->success('成功');
    }

    /**
     * Build a form here.
     */
    public function form()
    {
        $streets = Street::query()->where('hospital_id',Admin::user()->hospital_id)->pluck('title','id');
        $this->embeds('street','', function ($form) use($streets) {
            $form->multipleSelect('week1','周一')->placeholder('请选择街道')->options($streets);
            $form->multipleSelect('week2','周二')->placeholder('请选择街道')->options($streets);
            $form->multipleSelect('week3','周三')->placeholder('请选择街道')->options($streets);
            $form->multipleSelect('week4','周四')->placeholder('请选择街道')->options($streets);
            $form->multipleSelect('week5','周五')->placeholder('请选择街道')->options($streets);
            $form->multipleSelect('week6','周六')->placeholder('请选择街道')->options($streets);
            $form->multipleSelect('week0','周日')->placeholder('请选择街道')->options($streets);

        })->saving(function ($value) {
            return json_encode($value);
        });

        $this->hidden('hospital_appoint_id');
        $this->hidden('hospital_id');

    }

    /**
     * The data of the form.
     *
     * @return array
     */
    public function default()
    {
        return [
            'name'  => 'John Doe',
            'email' => 'John.Doe@gmail.com',
        ];
    }
}
